import fang from '../../models/Fang';
import Cache from '../../utils/Cache';

Page({
  data: {
    fangs: [],
    page: 1,
    topNum: 0,
    showTop: true,
    arrows: ['icon-xiangxia', 'icon-xiangxia', 'icon-xiangxia', 'icon-xiangxia'],
    show: ['', '', '', ''],
    showflag: [false, false, false, false],
    qattr: {},
    attrField:{}
  },
  onLoad(options) {
    this.setData({
      attrField: options
    })

    this.getMore();

    fang.quickAttr().then( ret => {
      this.setData({ qattr: ret.data.data });
    });
  },
  onShadeDiv (evt){
    // 获取当前点击的组件元素
    let index = evt.currentTarget.dataset.index;
    // 蒙层是否显示
    let show = this.data.show;
    // 判断显示与否
    let showflag = this.data.showflag;
    // 箭头样式 向上向下
    let arrows = this.data.arrows;

    if (showflag[index]) {
      show[index] = '';
      showflag[index] = false;
      arrows[index] = 'icon-xiangxia';
    } else {
      for (let key in show) {
        if (key == index) {
          show[key] = 'now';
          showflag[key] = true;
          arrows[key] = 'icon-xiangshang';
        } else {
          show[key] = '';
          showflag[key] = false;
          arrows[key] = 'icon-xiangxia';
        }
      }
    }

    this.setData({
      show, showflag, arrows
    });
  },
  getMore() {
    let page = this.data.page;
    // 房源列表
    if (!Cache.has('findmore')) {
      fang.getFangs(page, this.data.attrField).then( ret => {
        if (ret.data.data.data.length > 0) {
          this.setData({
            fangs: [ ...this.data.fangs, ...ret.data.data.data],
            page: ++page,
          });
        } else {
          Cache.set('findmore',true,10);
          wx.showToast({
            title: '没有更多最新的数据了',
            icon: 'none',
            duration: 1500,
            mask: true,
          });
        }
      });
    }
  },
  scrollTop(evt) {
    let scrollTop = evt.detail.scrollTop;
    
    let showTop = true;

    if (scrollTop >= 200) {
      showTop = false;
    }

    this.setData({ showTop });
  },
  goTo() {
    this.setData({
      topNum: 0
    })
  }
})